﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TaskListManager
{
   public partial class TaskListManager : Form
   {
      TaskUserInterface m_sDataInterface;
      public TaskListManager()
      {
         m_sDataInterface = new TaskUserInterface();
         InitializeComponent();
      }

      private void TaskListManager_FormClosing(object sender, FormClosingEventArgs e)
      {
         m_sDataInterface.SaveToDB();
      }

      private void TaskListManager_Load(object sender, EventArgs e)
      {
         m_sDataInterface.LoadFromDB();
         tabCtrlMainSelection.SelectedIndex = 0;
         tabCtrlMainSelection.SelectedIndex = 1;
         tabCtrlMainSelection.SelectedIndex = 2;
         tabCtrlMainSelection.SelectedIndex = 0;
      }

      private void cmdAddUser_Click(object sender, EventArgs e)
      {
         string strName = txtNewUserName.Text.Trim();
         bool bExists = m_sDataInterface.UserExists(strName);
         if ((bExists == false) && (strName.Length > 1))
         {
            m_sDataInterface.AddUser(strName);
            lstUserList.Items.Add(strName);
         }
      }

      private void cmdModifyUserName_Click(object sender, EventArgs e)
      {
         int lSelected = lstUserList.SelectedIndex;
         if ((lSelected > -1) && (lSelected < lstUserList.Items.Count))
         {
            string strSelectedName = lstUserList.Items[lSelected].ToString();
            int lUserId = m_sDataInterface.GetUserIdFromName(strSelectedName);
            string strNewName = txtModifyUserName.Text.Trim();
            bool bUserExists = m_sDataInterface.UserExists(strNewName);
            if ((bUserExists == false) && (strNewName.Length > 1))
            {
               m_sDataInterface.RenameUser(lUserId, strNewName);
               lstUserList.Items[lSelected] = strNewName;
            }
         }
      }

      private void cmdDeleteUser_Click(object sender, EventArgs e)
      {
         int lSelected = lstUserList.SelectedIndex;
         if ((lSelected > -1) && (lSelected < lstUserList.Items.Count))
         {
            m_sDataInterface.RemoveUser(lstUserList.Items[lSelected].ToString());
            lstUserList.Items.RemoveAt(lSelected);
         }
      }

      private void tabCtrlMainSelection_SelectedIndexChanged(object sender, EventArgs e)
      {
         if (tabCtrlMainSelection.SelectedTab.Tag.ToString() == "REPORTS")
         {

         }
         else if (tabCtrlMainSelection.SelectedTab.Tag.ToString() == "USER_MODIFY")
         {
            UpdateUserNamesInUserPage();
         }
         else if (tabCtrlMainSelection.SelectedTab.Tag.ToString() == "TASK_MODIFY")
         {
            UpdateUserNamesInTaskManagementPage();
            UpdateTaskEntriesInTaskManagementPage();
         }
      }

      private DataTable ConvertTaskEntryListToDataTable(TaskEntry[] sEntries)
      {
         DataTable sData = new DataTable();
         DataColumn sOwnerName = new DataColumn("Owner Name");
         sData.Columns.Add(sOwnerName);
         DataColumn sDesc = new DataColumn("Description", typeof(string));
         sData.Columns.Add(sDesc);
         DataColumn sStartDate = new DataColumn("Start Date", typeof(DateTime));
         sData.Columns.Add(sStartDate);
         DataColumn sEndDate = new DataColumn("End Date", typeof(DateTime));
         sData.Columns.Add(sEndDate);
         DataColumn sPercentComplete = new DataColumn("Percent Complete", typeof(short));
         sData.Columns.Add(sPercentComplete);
         DataColumn sCompleted = new DataColumn("Completed", typeof(bool));
         sData.Columns.Add(sCompleted);
         DataColumn sUserId = new DataColumn("UserId", typeof(int));
         sData.Columns.Add(sUserId);
         DataColumn sTaskId = new DataColumn("TaskId", typeof(int));
         sData.Columns.Add(sTaskId);
         foreach (TaskEntry sEntry in sEntries)
         {
            DataRow sDataRow = sData.NewRow();
            string strUserName = m_sDataInterface.GetUserNameFromId(sEntry.OwnerUser);
            sDataRow.SetField(sOwnerName, strUserName);
            sDataRow.SetField(sStartDate, new DateTime(sEntry.StartDate));
            sDataRow.SetField(sEndDate, new DateTime(sEntry.EndDate));
            sDataRow.SetField(sPercentComplete, sEntry.PercentComplete);
            sDataRow.SetField(sCompleted, sEntry.Complete);
            sDataRow.SetField(sDesc, sEntry.Description);
            sDataRow.SetField(sUserId, sEntry.OwnerUser);
            sDataRow.SetField(sTaskId, sEntry.TaskId);
            sData.Rows.Add(sDataRow);
         }
         return sData;
      }

      private void UpdateTaskEntriesInTaskManagementPage()
      {
         dataGridTaskFullList.DataSource = null;
         dataGridTaskFullList.DataSource = ConvertTaskEntryListToDataTable(m_sDataInterface.GetAllTasks());
      }

      private void UpdateUserNamesInTaskManagementPage()
      {
         string[] strUsers = m_sDataInterface.GetUserNames();
         foreach (string strName in cboNewTaskUserList.Items)
         {
            bool bExists = false;
            for (int i = 0; i < strUsers.Count(); i++)
            {
               if (strUsers[i] == strName)
               {
                  bExists = true;
                  break;
               }
            }
            if (bExists == false)
            {
               cboNewTaskUserList.Items.Remove(strName);
            }
         }
         for (int i = 0; i < strUsers.Count(); i++)
         {
            if (strUsers[i].Length > 1)
            {
               int lIdx = cboNewTaskUserList.Items.IndexOf(strUsers[i]);
               if ((lIdx < 0) || (lIdx > cboNewTaskUserList.Items.Count))
               {
                  cboNewTaskUserList.Items.Add(strUsers[i]);
               }
            }
         }
         foreach (string strName in cboModifyTaskUserList.Items)
         {
            bool bExists = false;
            for (int i = 0; i < strUsers.Count(); i++)
            {
               if (strUsers[i] == strName)
               {
                  bExists = true;
                  break;
               }
            }
            if (bExists == false)
            {
               cboModifyTaskUserList.Items.Remove(strName);
            }
         }
         for (int i = 0; i < strUsers.Count(); i++)
         {
            if (strUsers[i].Length > 1)
            {
               int lIdx = cboModifyTaskUserList.Items.IndexOf(strUsers[i]);
               if ((lIdx < 0) || (lIdx > cboModifyTaskUserList.Items.Count))
               {
                  cboModifyTaskUserList.Items.Add(strUsers[i]);
               }
            }
         }
      }

      private void UpdateUserNamesInUserPage()
      {
         string[] strUsers = m_sDataInterface.GetUserNames();
         foreach (string strName in lstUserList.Items)
         {
            bool bExists = false;
            for (int i = 0; i < strUsers.Count(); i++)
            {
               if (strUsers[i] == strName)
               {
                  bExists = true;
                  break;
               }
            }
            if (bExists == false)
            {
               lstUserList.Items.Remove(strName);
            }
         }
         for (int i=0; i<strUsers.Count(); i++)
         {
            if (strUsers[i].Length > 1)
            {
               int lIdx = lstUserList.Items.IndexOf(strUsers[i]);
               if ((lIdx < 0) || (lIdx > lstUserList.Items.Count))
               {
                  lstUserList.Items.Add(strUsers[i]);
               }
            }
         }
         
      }

      private void cmdCreateNewTask_Click(object sender, EventArgs e)
      {
         long lStartDate = dateTimeNewTaskStartDate.Value.Ticks;
         long lEndDate = dateTimeNewTaskEndDate.Value.Ticks;
         short nPercentComplete = (short)numUpDownNewTaskPercentComplete.Value;
         bool bTaskCompleted = chkNewTaskAlreadyCompleted.Checked;
         string strUser = cboNewTaskUserList.SelectedItem.ToString();
         int lUserId = m_sDataInterface.GetUserIdFromName(strUser);
         string strTaskDesc = txtNewTaskDescription.Text.Trim();
         if ((lEndDate > lStartDate) && (nPercentComplete >= 0) && (nPercentComplete <= 100) && (lUserId != -1) && (strTaskDesc.Length > 1))
         {
            m_sDataInterface.AddTask(strTaskDesc, lStartDate, lEndDate, nPercentComplete, bTaskCompleted, lUserId);
            UpdateTaskEntriesInTaskManagementPage();
         }
      }

      private void chkNewTaskAlreadyCompleted_CheckedChanged(object sender, EventArgs e)
      {
         if (chkNewTaskAlreadyCompleted.Checked == true)
         {
            numUpDownNewTaskPercentComplete.Value = 100;
         }
      }

      private void numUpDownNewTaskPercentComplete_ValueChanged(object sender, EventArgs e)
      {
         if (chkNewTaskAlreadyCompleted.Checked == true)
         {
            numUpDownNewTaskPercentComplete.Value = 100;
         }
      }

      private void chkModifyTaskAlreadyCompleted_CheckedChanged(object sender, EventArgs e)
      {
         if (chkModifyTaskAlreadyCompleted.Checked == true)
         {
            numUpDownModifyTaskPercentComplete.Value = 100;
         }
      }

      private void numUpDownModifyTaskPercentComplete_ValueChanged(object sender, EventArgs e)
      {
         if (chkModifyTaskAlreadyCompleted.Checked == true)
         {
            numUpDownModifyTaskPercentComplete.Value = 100;
         }
      }

      private void cmdModify_Click(object sender, EventArgs e)
      {
         string strUserName = cboModifyTaskUserList.SelectedItem.ToString();
         bool bExists = m_sDataInterface.UserExists(strUserName);
         string strDesc = txtModifyTaskDescription.Text;
         long lStartDate = dateTimeModifyTaskStartDate.Value.Ticks;
         long lEndDate = dateTimeModifyTaskEndDate.Value.Ticks;
         short nPercentComplete = (short)numUpDownModifyTaskPercentComplete.Value;
         bool bCompleted = chkModifyTaskAlreadyCompleted.Checked;
         int lTaskId = int.Parse(txtCurrentTaskIdentifier.Text);
         int lUserId = int.Parse(txtCurrentOwnerIdentifier.Text);
         if (m_sDataInterface.TaskExists(lTaskId) == true)
         {
            TaskEntry sEntry = m_sDataInterface.GetTaskWithId(lTaskId);
            if ((bExists == true) && (lUserId != sEntry.OwnerUser))
            {
               m_sDataInterface.MoveTaskFromUser(lUserId, m_sDataInterface.GetUserIdFromName(strUserName), lTaskId);
            }
            if (sEntry.Description.Equals(strDesc, StringComparison.Ordinal) == false)
            {
               m_sDataInterface.ModifyTaskDesc(lTaskId, strDesc);
            }
            if (sEntry.StartDate != lStartDate)
            {
               m_sDataInterface.ModifyTaskStartDate(lTaskId, lStartDate);
            }
            if (sEntry.EndDate != lEndDate)
            {
               m_sDataInterface.ModifyTaskEndDate(lTaskId, lEndDate);
            }
            if (sEntry.PercentComplete != nPercentComplete)
            {
               m_sDataInterface.ModifyTaskPercentComplete(lTaskId, nPercentComplete);
            }
            if (sEntry.Complete != bCompleted)
            {
               m_sDataInterface.ModifyTaskCompleted(lTaskId, bCompleted);
            }
         }
         UpdateTaskEntriesInTaskManagementPage();
      }

      private void cmdDelete_Click(object sender, EventArgs e)
      {
         int lUserId = -1;
         int lTaskId = -1;
         if ((dataGridTaskFullList.SelectedRows != null) && (dataGridTaskFullList.SelectedRows.Count == 1))
         {
            foreach (DataGridViewCell sCell in dataGridTaskFullList.SelectedRows[0].Cells)
            {
               if (dataGridTaskFullList.Columns[sCell.ColumnIndex].Name == "UserId")
               {
                  lUserId = (int)sCell.Value;
               }
               else if (dataGridTaskFullList.Columns[sCell.ColumnIndex].Name == "TaskId")
               {
                  lTaskId = (int)sCell.Value;
               }
            }
         }
         if ((lUserId != -1) && (lTaskId != -1))
         {
            if ((m_sDataInterface.UserExists(lUserId) == true) && (m_sDataInterface.TaskExists(lTaskId) == true))
            {
               m_sDataInterface.RemoveTaskForUser(lUserId, lTaskId);
               UpdateTaskEntriesInTaskManagementPage();
            }
         }
      }

      private void dataGridTaskFullList_SelectionChanged(object sender, EventArgs e)
      {
         if ((dataGridTaskFullList.SelectedRows != null) && (dataGridTaskFullList.SelectedRows.Count == 1))
         {
            foreach (DataGridViewCell sCell in dataGridTaskFullList.SelectedRows[0].Cells)
            {
               if (dataGridTaskFullList.Columns[sCell.ColumnIndex].Name == "Owner Name")
               {
                  cboModifyTaskUserList.SelectedItem = sCell.Value;
                  txtCurrentOwnerName.Text = sCell.Value.ToString();
               }
               else if (dataGridTaskFullList.Columns[sCell.ColumnIndex].Name == "Description")
               {
                  txtModifyTaskDescription.Text = sCell.Value.ToString();
               }
               else if (dataGridTaskFullList.Columns[sCell.ColumnIndex].Name == "Start Date")
               {
                  dateTimeModifyTaskStartDate.Value = (DateTime)sCell.Value;
               }
               else if (dataGridTaskFullList.Columns[sCell.ColumnIndex].Name == "End Date")
               {
                  dateTimeModifyTaskEndDate.Value = (DateTime)sCell.Value;
               }
               else if (dataGridTaskFullList.Columns[sCell.ColumnIndex].Name == "Percent Complete")
               {
                  numUpDownModifyTaskPercentComplete.Value = (short)sCell.Value;
               }
               else if (dataGridTaskFullList.Columns[sCell.ColumnIndex].Name == "Completed")
               {
                  chkModifyTaskAlreadyCompleted.Checked = (bool)sCell.Value;
               }
               else if (dataGridTaskFullList.Columns[sCell.ColumnIndex].Name == "UserId")
               {
                  txtCurrentOwnerIdentifier.Text = sCell.Value.ToString();
               }
               else if (dataGridTaskFullList.Columns[sCell.ColumnIndex].Name == "TaskId")
               {
                  txtCurrentTaskIdentifier.Text = sCell.Value.ToString();
               }
            }
         }
         else
         {
            txtCurrentTaskIdentifier.Text = "";
            txtCurrentOwnerIdentifier.Text = "";
            chkModifyTaskAlreadyCompleted.Checked = false;
            numUpDownModifyTaskPercentComplete.Value = 0;
            dateTimeModifyTaskEndDate.Value = DateTime.Now;
            dateTimeModifyTaskStartDate.Value = DateTime.Now;
            txtModifyTaskDescription.Text = "";
            txtCurrentOwnerName.Text = "";
            cboModifyTaskUserList.SelectedIndex = -1;
         }
      }
   }
}
